package com.zmh.thread.muilt_thread_print;

/**
 * ���߳̽����ӡ ������һ���ࣩ
 *
 * @author zmh
 * @create 2018-10-24 ����11:11
 **/
public class MuiltThreadPrint_2 extends Thread{
    String content;
    Object prev;
    Object self;

    public MuiltThreadPrint_2(String content, Object prev, Object self) {
        this.content = content;
        this.prev = prev;
        this.self = self;
    }

    @Override
    public void run() {
        int count = 10;
        while (count > 0) {
            synchronized (prev) {
                synchronized (self) {
                    System.out.println(content);
                    count--;
                    self.notifyAll();
                }
                try {
                    if (count == 0) {
                        prev.notifyAll();
                    } else {
                        prev.wait();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }



    }

    public static void main(String[] args) throws InterruptedException {
        Object a = new Object();
        Object b = new Object();
        Object c = new Object();
        MuiltThreadPrint_2 muiltThreadPrintA = new MuiltThreadPrint_2("a", c, a);
        MuiltThreadPrint_2 muiltThreadPrintB = new MuiltThreadPrint_2("b", a, b);
        MuiltThreadPrint_2 muiltThreadPrintC = new MuiltThreadPrint_2("c", b, c);
        muiltThreadPrintA.start();
        Thread.sleep(100);
        muiltThreadPrintB.start();
        Thread.sleep(100);
        muiltThreadPrintC.start();
    }
}

